<?php
/*
 * Created on 13.01.2006
 *
 */
 
 
class c_includeHandler 
{
    
    // TODO fix with use-compiledir
    function startElement (& $sender, $attrs)
    {
        if (!$attrs['page'])
        {
            die ("Attribute page are missing at include tag\n");
        }
        $code = '
                $viewparts = explode(DIRECTORY_SEPARATOR, dirname($dispatcher->getInitialBaseDir().$this->relativeFilePath));
                $page = explode(DIRECTORY_SEPARATOR, '.$attrs['page'].');
                while ($page[0] == \'..\')
                {
                    array_shift($page);
                    array_pop($viewparts);
                }
                $view  = join(DIRECTORY_SEPARATOR, $viewparts).DIRECTORY_SEPARATOR.join(DIRECTORY_SEPARATOR, $page);
                if ($dispatcher->hasCompileDir()) {
                    $viewC = $dispatcher->getCompileDir().$dispatcher->getPSPClassName($view).\'.psp.php\';
                }        
                else       
                {        
                    $viewC = $view.\'.php\';
                }        
                if (file_exists($view)) 
                {
                    if (file_exists($viewC) && !MODEL2_FORCE_COMPILE)
                    {
                        $cfile = stat($viewC);
                        $sfile = stat($view);
                        // mtime comparison
                        if ($cfile[9] < $sfile[9])
                            $this->compilePSP($view, 
                                $viewC, $dispatcher->getPSPClassName($view));
                    }
                    else
                    {
                        $this->compilePSP($view, 
                            $viewC, $dispatcher->getPSPClassName($view));
                    }
                    if ($dispatcher->isPSPClassCompiler())
                    {
                        include_once($viewC);
                    ';
        $codeBottom = '    $classname = $dispatcher->getPSPClassName($view)."PSPPage";
                        $obj = new $classname();
                        $obj->render($dispatcher, $env, $actionform, $basedir);
                    } else {
                        include($viewC);
                    }
                }
                else
                    $out->write("<b>PSP File ".$view." not found!</b></br>");'."\n";
        $codeBottomVal = '    $classname = $dispatcher->getPSPClassName($view)."PSPPage";
                        $obj = new $classname();
                        $returnVal = ($returnVal && $obj->validate($dispatcher, $env, $actionform, $formname, $basedir));                    
                    }
                }
                else
                    $out->write("<b>PSP File '.$attrs['page'].' not found!</b></br>");'."\n";
        $codeBottomJS = '    $classname = $dispatcher->getPSPClassName($view)."PSPPage";
                        $obj = new $classname();
                        $jscode .= $obj->getJSCode($dispatcher, $env, $actionform);
                    }                    
                }
                else
                    $jscode .= "alert(\"PSP File '.$attrs['page'].' not found!\");";
        '."\n";
        $sender->appendCode($code.$codeBottom);
        $sender->appendJSCode('";'.$code.$codeBottomJS.' $jscode .= "', false);
        $sender->appendValidationCode($code.$codeBottomVal);
    }
    
    function endElement (& $sender)
    {
    }

}

?>
